package com.portal.back.service.impl;

import java.util.List;

import com.portal.back.service.ISysAdminService;
import com.portal.base.dao.BaseDao;
import com.portal.base.dao.IBaseDao;
import com.portal.domain.SysAdminUser;

/**
 * @Title:管理员service
 * @Copyright: Copyright (c) 0.1
 * @author:鲁根成
 * @version: 1.0
 * @time: 2010-3-25
 * 
 */

public class SysAdminService implements  ISysAdminService {
	
	private  IBaseDao<SysAdminUser, Integer>  dao;
	public    SysAdminService(){
		dao=new BaseDao<SysAdminUser, Integer>(SysAdminUser.class);

	}
	/**
	 * 
	 * @param name 登录的用户名
	 * @param password 登录的密码
	 * @return 用户对象
	 * @author:lgc
	 */
	 public SysAdminUser getByNameAndPass(String name, String password) {

		   String sql="select *   from  sys_adminuser where adminName='"+ name+ "' and  password='"+password+"'" ;;
	       List<SysAdminUser>  list= dao.findList(sql);
	       if(list!=null && list.size()!=0){
	    	   return  list.get(0);
	    	   
	       }
		return null;
	}
	 public SysAdminUser getLoginSuccess(String name, String password,
				String ip, String date) {
			 SysAdminService  service=new  SysAdminService();
			 SysAdminUser user=service.getByNameAndPass(name, password);
			 if(user!=null){
				boolean  isFlag=service.updateIPAndTime(name, password, ip, date);
				if(isFlag) return  user;
				return null;
			 }
			return null;
		}
		public boolean updateIPAndTime(String name, String password,String ip, String date) {
			String sql="update sys_adminuser   set lastLoginIp=?, lastLoginTime=?  where  adminName=? and  password=?";
			try{  dao.update(sql, new Object[]{ip,date,name,password});
			      return  true;
			}catch (Exception e) {
			    return false;
			}
			
		}
		/**
	       * 更改管理员密码
	       * @param user
	       * @return
	       */
	      public   boolean      updatePass(SysAdminUser user){
	    	  try{
	    	  dao.update(user);
	    	  return true;
	    	  }catch (Exception e) {
	    		  
				 e.printStackTrace();
				 return false;
			}
	    	 
	      }
	      public   String       getLeveleById(String id){
	    	  String  sql="select  * from  sys_adminuser where  id=?";
	    	 List<SysAdminUser>  list=dao.findList(sql,new Object[]{id});
	    	 if(list!=null && list.size()>0){
	    		 SysAdminUser user=list.get(0);
	    		 return  user.getLevel();
	    	 }
	    	 return null;
	      }
	      public   int  count(String name){
	    	    String sql="select  * from  sys_adminuser where  adminName=? ";
	    	    List<SysAdminUser>  list=dao.findList(sql,new Object[]{name});
	    	    if(list!=null && list.size()>0)return 1;
		    	 return 0;
	      }
	      public  boolean  save(SysAdminUser user){
	    	  try {
	    		String  sql="insert into sys_adminuser(adminName,password,level) values(?,?,?) ";
				 dao.insert(sql, new Object[]{user.getAdminName(),user.getPassword(),user.getLevel()});
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
	      }
}
